<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    .adBox {
      width: 300px;
      height: 400px;
      background-color: #6ff;
      position: fixed;
      right: 5px;
      bottom: 5px;
      display: none;
    }

    .adBox .close {
      position: absolute;
      right: 0;
      top: 0;
      background-color: red;
      color: #fff;
      padding: 2px;
    }
  </style>
</head>

<body>
  <div class="adBox">
    <div class="tips">距离广告关闭还有10秒</div>
    <div class="close">X</div>
  </div>
</body>
<script>
  var adBox = document.querySelector(".adBox");
  var tips = document.querySelector(".tips");
  var closeBtn = document.querySelector(".close");
  var Atimer = null;
  var Btimer = null;

  fn1(3000);

  // 点击关闭 1. 隐藏小广告  2. 清除延时器  3. 清除计时器
  closeBtn.onclick = function () {
    adBox.style.display = "none";
    clearInterval(Btimer);
    clearTimeout(Atimer);
  }

  function fn1(time) {
    Atimer = setTimeout(function () {
      adBox.style.display = "block";
      console.log('广告弹出！！！');

      //倒计时
      var num = 10;
      tips.innerHTML = `距离广告关闭还有${num}秒`;
      Btimer = setInterval(function () {
        num--;
        tips.innerHTML = `距离广告关闭还有${num}秒`;
        if (num == 0) {
          clearInterval(Btimer);
          adBox.style.display = "none";
          fn1(time);
        }

      }, 1000)

    }, time)

  }

</script>

</html>